package com.kky.pp.popup

import android.app.Activity
import android.view.Gravity
import android.view.View
import android.widget.FrameLayout
import android.widget.TextView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.custom.base.entity.OkHttpBodyEntity
import com.custom.base.http.OkHttpFromBoy
import com.custom.base.http.OkHttpManager
import com.custom.base.http.SDOkHttpResoutCallBack
import com.custom.base.view.SuperViewHolder
import com.kky.pp.common.Constant
import com.kky.pp.R
import com.kky.pp.bean.TOListBean
import com.kky.pp.entity.UserProfileEntity
import com.kky.pp.view.ProfileInfoTurnOnsView
import com.kky.pp.util.CollectionUtils
import com.kky.pp.util.DensityUtil
import com.kky.pp.view.Loading.LoadingDialog

object TOListPopup {

    private var listener:OnClickSaveListener?=null
    private var isCanceled:Boolean = true
    fun openDialog(activity: Activity, turnOnsData: ArrayList<TOListBean>? = null):TOListPopup {
        if (turnOnsData.isNullOrEmpty()) {
            requestData(activity)
        } else {
            show(activity, turnOnsData)
        }
        return this
    }
    fun setCanceledOnTouchOutside(isCanceled:Boolean):TOListPopup {
        this.isCanceled = isCanceled
        return this
    }

    private fun requestData(activity: Activity) {
        LoadingDialog.showLoading(activity)
        OkHttpManager.instance.requestInterface(object : OkHttpFromBoy {
            override fun addBody(requestBody: OkHttpBodyEntity) {
                requestBody.setPost(Constant.user_info_url)
            }
        }, object : SDOkHttpResoutCallBack<UserProfileEntity>() {
            override fun onSuccess(entity: UserProfileEntity) {
                for (i in 0 until entity.data.images.size) {
                    Glide.with(activity)
                        .load(entity.data.images[i])
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .preload()
                }

                if (CollectionUtils.isNotEmpty(entity.data.turnOnsList)) {
                    show(activity, entity.data.turnOnsList)
                }
            }

            override fun onFailure(code: Int, msg: String) {

            }

            override fun onFinish() {
                super.onFinish()
                LoadingDialog.dismissLoading(activity)
            }
        })
    }

    private fun show(activity: Activity, turnOnsData: ArrayList<TOListBean>) {
        val TOListBeans = arrayListOf<TOListBean>()
        turnOnsData.stream().forEach {
            val TOListBean = TOListBean(it.turnOnsCode,it.title,it.content,it.imageUrl,it.selected)
            TOListBeans.add(TOListBean)
        }
     val dialog = CustomPopup(activity)
            .setGravity(Gravity.BOTTOM)
            .setLayoutId(R.layout.popup_t_o_list)
            .setControllerListener { dialog ->
                val profileInfoTurnOnsView =
                    dialog.findViewById<ProfileInfoTurnOnsView>(R.id.viewTurnOns)
                profileInfoTurnOnsView.setPadding()
                //profileInfoTurnOnsView.viewMian.setPadding(0, 0, 0, 0)
                dialog.findViewById<FrameLayout>(R.id.viewMore).visibility = View.GONE
                profileInfoTurnOnsView.adapter?.top =
                    DensityUtil.dip2px(activity, 20f)
                profileInfoTurnOnsView.setData(TOListBeans)
                if (isCanceled){
                    dialog.findViewById<View>(R.id.dialogViewMain)
                        .setOnClickListener { dialog.dismiss() }
                }
                val tvSave = dialog.findViewById<TextView>(R.id.txt_save)
                val dataList = profileInfoTurnOnsView?.adapter?.dataList
                tvSave.setOnClickListener {
                    if (!dataList.isNullOrEmpty()) {
                        saveProfileInfo(turnOns = dataList as ArrayList<TOListBean>)
                        dialog.dismiss()
                        listener?.clickSave(dataList)
                    }
                }
                profileInfoTurnOnsView.setBgNull()
                //profileInfoTurnOnsView.viewTurnOnsBg?.background=null

                profileInfoTurnOnsView.adapter?.setOnItemClickListener(object :
                    ProfileInfoTurnOnsView.Adapter.OnItemClickListener {
                    override fun onItemClick(
                        position: Int,
                        holder: SuperViewHolder,
                        model: TOListBean
                    ) {
                        model.selected = if (model.selected == 0) 1 else 0
                        profileInfoTurnOnsView.adapter?.notifyItemChanged(position)
                    }
                })
            }.show()
    }

    private fun saveProfileInfo(
        turnOns: java.util.ArrayList<TOListBean> = arrayListOf(),
    ) {
        OkHttpManager.instance.requestInterface(object : OkHttpFromBoy {
            override fun addBody(requestBody: OkHttpBodyEntity) {
                requestBody.setPost(Constant.user_user_change_url)
                if (turnOns.isNotEmpty()) {
                    val list = arrayListOf<Int>()
                    repeat(turnOns.size) {
                        if (turnOns[it].selected == 1) {
                            list.add(turnOns[it].turnOnsCode)
                        }
                    }
                    requestBody.add("turnOnsList", list)
                }

            }
        }, object : SDOkHttpResoutCallBack<UserProfileEntity>() {
            override fun onSuccess(entity: UserProfileEntity) {

            }

            override fun onFailure(code: Int, msg: String) {
            }
        })
    }

    fun setOnClickSaveListener(listener:OnClickSaveListener){
        this.listener = listener
    }

    interface OnClickSaveListener{
        fun clickSave(dataList:ArrayList<TOListBean>)
    }

}